package cn.yshujia.controller;

import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.Comment;
import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.domain.vo.CommentVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.service.impl.CommentServiceImpl;
import com.alibaba.fastjson2.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 控制器
 */

@RestController
@Tag (name = "Comment", description = "评论Api")
public class CommentController {
	
	@Resource
	private CommentServiceImpl service;
	
	@PostMapping ("/comment/insert")
	@Operation (summary = "添加评论")
	@PreAuthorize ("@sys.hasOnePermission('comment:all', 'comment:insert')")
	public ApiVO<Boolean> insert(@RequestParam (value = "files", required = false) MultipartFile files, @RequestBody Comment dto) throws IOException {
		if (null == files && null == dto) {
			return ApiVO.error();
		}
		return ApiVO.success(service.insert(files, dto));
	}
	
	@GetMapping ("/comment/page")
	@Operation (summary = "根据查询条件获取评论分页")
	public ApiVO<PageVO<CommentVO>> page(@RequestParam String dto) {
		PageDTO dtoPage = JSON.parseObject(dto, PageDTO.class);
		if (null == dtoPage) {
			return ApiVO.error();
		}
		return ApiVO.success(service.selectPage(dtoPage));
	}
	
	@GetMapping ("/admin/comment/page")
	@Operation (summary = "admin 根据查询条件获取评论分页")
	@PreAuthorize ("@sys.hasOnePermission('comment:all')")
	public ApiVO<PageVO<CommentVO>> adminPage(@RequestParam String dto) {
		PageDTO dtoPage = JSON.parseObject(dto, PageDTO.class);
		if (null == dtoPage) {
			return ApiVO.error();
		}
		return ApiVO.success(service.adminPage(dtoPage));
	}
	
	@DeleteMapping ("/admin/comment/delete")
	@Operation (summary = "admin 删除评论，将评论内容修改为 该评论已删除")
	@PreAuthorize ("@sys.hasOnePermission('comment:all')")
	public ApiVO<Boolean> delete(@RequestBody List<Long> ids) {
		Boolean result = service.remove(ids);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
}
